package com.ruoyi.domain;

import com.alibaba.fastjson2.JSONObject;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class BaiduAIResponse {

    private String id; // 本轮对话id

    private String object; // 回包类型  chat.completion: 多轮对话返回

    private int created; // 时间戳

    private int sentenceId; // 表示当子句的序号。只有在流失接口模式下会返回该字段 序号从0开始

    private boolean isEnd; // 表示当前子句是否是最后一句。只有在流式接口下会返回该字段

    private boolean isTruncated; // 对当前生成的结果是否被截断

    private String result; // 对话返回结果

    private boolean needClearHistory; // 表示用户输入是否存在安全, 是否关闭当前会话, 清理历史回话信息

    private int banRound; // 当needClearHistory为true时, 此字段会告知第几轮对话有敏感信息, 如果是当前问题, banRound=1

    // 传递一个json, 解析成BaiDuAiResponse对象
    public BaiduAIResponse(String jsonString) {
        try {
            if (jsonString.startsWith("data")){
                jsonString = jsonString.replace("data: ", "");
            }
            final JSONObject jsonObject = JSONObject.parseObject(jsonString);
            id = jsonObject.getString("id");
            object = jsonObject.getString("object");
            created = jsonObject.getInteger("created");
            sentenceId = jsonObject.getInteger("sentence_id");
            isEnd = jsonObject.getBoolean("is_end");
            isTruncated = jsonObject.getBoolean("is_truncated");
            result = jsonObject.getString("result");
            needClearHistory = jsonObject.getBoolean("need_clear_history");
            if (needClearHistory == true) {
                banRound = jsonObject.getInteger("ban_round");
            }
        }catch (Exception e) {
            e.printStackTrace();
            System.out.println("解析json失败");
        }
    }
}
